package com.gujiangbo.juc;

import sun.misc.Unsafe;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/6/10 16:05
 * @Version: 1.0
 * @Description:
 */
public class LockSupportDemo {
    public static void main(String[] args) {
        //waitNotify();
        //lockCondition();
        lockSupportPark();

    }

    private static void lockSupportPark() {
        Thread t1 = new Thread(() -> {
            //暂停5秒钟线程
           // try{TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(Thread.currentThread().getName() + "\t" + "开始等待..");
            LockSupport.park();// UNSAFE.park(false, 0L);
            //LockSupport.park();// UNSAFE.park(false, 0L);

            System.out.println(Thread.currentThread().getName() + "\t"+"被唤醒......");
        }, "t1");
        Thread t3 = new Thread(() -> {
            //暂停5秒钟线程
            //try{TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(Thread.currentThread().getName() + "\t" + "开始等待..");
            LockSupport.park();// UNSAFE.park(false, 0L);

            System.out.println(Thread.currentThread().getName() + "\t"+"被唤醒......");
        }, "t3");
        t1.start();
       // t3.start();
       /* try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" +"开始唤醒");
            LockSupport.unpark(t1);//UNSAFE.unpark(thread);
            //LockSupport.unpark(t1);//UNSAFE.unpark(thread);
            System.out.println(Thread.currentThread().getName()+"\t"+"唤醒结束");
        }, "t2").start();
    }

    private static void lockCondition() {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t" + "is coming in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t" + "被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1").start();

        //暂停5秒钟线程
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t is coming in");
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t 开始唤醒");
            } finally {
                lock.unlock();
            }
        }, "t2").start();
    }

    private static void waitNotify() {
        Object objectLock = new Object();

        new Thread(() -> {
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + " is coming in .");
                try {
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "被唤醒.....");
            }
        }, "t1").start();

        //暂停5秒钟线程
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            synchronized (objectLock) {
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + " 发出唤醒通知.");
            }
        }, "t2").start();
    }
}
